package com.settlement.system.service.impl;

import com.settlement.system.model.entity.OdsErpRetailWholesaleOrderDetails;
import com.settlement.system.mapper.OdsErpRetailWholesaleOrderDetailsMapper;
import com.settlement.system.service.OdsErpRetailWholesaleOrderDetailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.OdsErpRetailWholesaleOrderDetailsQuery;
import com.settlement.system.model.form.OdsErpRetailWholesaleOrderDetailsForm;
import com.settlement.system.model.vo.OdsErpRetailWholesaleOrderDetailsImportVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;

/**
 * <p>
 * 批发销货单明细表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-08-02
 */
@Service
public class OdsErpRetailWholesaleOrderDetailsServiceImpl extends ServiceImpl<OdsErpRetailWholesaleOrderDetailsMapper, OdsErpRetailWholesaleOrderDetails> implements OdsErpRetailWholesaleOrderDetailsService {

    @Override
    public Page<OdsErpRetailWholesaleOrderDetails> getPage(OdsErpRetailWholesaleOrderDetailsQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<OdsErpRetailWholesaleOrderDetails> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveOdsErpRetailWholesaleOrderDetails(OdsErpRetailWholesaleOrderDetailsForm odsErpRetailWholesaleOrderDetailsForm) {
         OdsErpRetailWholesaleOrderDetails odsErpRetailWholesaleOrderDetails = new OdsErpRetailWholesaleOrderDetails();
         BeanUtils.copyProperties(odsErpRetailWholesaleOrderDetailsForm, odsErpRetailWholesaleOrderDetails);
         return this.saveOrUpdate(odsErpRetailWholesaleOrderDetails);
     }

     @Override
     public OdsErpRetailWholesaleOrderDetailsForm getOdsErpRetailWholesaleOrderDetailsFormData(Long id) {
         OdsErpRetailWholesaleOrderDetails odsErpRetailWholesaleOrderDetails = this.getById(id);
         OdsErpRetailWholesaleOrderDetailsForm odsErpRetailWholesaleOrderDetailsForm = new OdsErpRetailWholesaleOrderDetailsForm();
         BeanUtils.copyProperties(odsErpRetailWholesaleOrderDetails, odsErpRetailWholesaleOrderDetailsForm);
         return odsErpRetailWholesaleOrderDetailsForm;
     }

     @Override
     public boolean deleteOdsErpRetailWholesaleOrderDetails(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<OdsErpRetailWholesaleOrderDetailsImportVo> voList)  {
        List<OdsErpRetailWholesaleOrderDetails> odsErpRetailWholesaleOrderDetailsList = new ArrayList<>();
        for (OdsErpRetailWholesaleOrderDetailsImportVo importVo : voList) {
            OdsErpRetailWholesaleOrderDetails odsErpRetailWholesaleOrderDetails = new OdsErpRetailWholesaleOrderDetails();
            BeanUtils.copyProperties(importVo, odsErpRetailWholesaleOrderDetails);
            odsErpRetailWholesaleOrderDetailsList.add(odsErpRetailWholesaleOrderDetails);
        }
        this.saveOrUpdateBatch(odsErpRetailWholesaleOrderDetailsList);
    }

    @Override
    public List<OdsErpRetailWholesaleOrderDetailsImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<OdsErpRetailWholesaleOrderDetails> odsErpRetailWholesaleOrderDetailsList = this.getBaseMapper().selectBatchIds(ids);
        List<OdsErpRetailWholesaleOrderDetailsImportVo> voList = new ArrayList<>();
        for (OdsErpRetailWholesaleOrderDetails odsErpRetailWholesaleOrderDetails : odsErpRetailWholesaleOrderDetailsList) {
            OdsErpRetailWholesaleOrderDetailsImportVo vo = new OdsErpRetailWholesaleOrderDetailsImportVo();
            BeanUtils.copyProperties(odsErpRetailWholesaleOrderDetails, vo);
            voList.add(vo);
        }
        return voList;
    }
}
